package design_pattern.singleton;

import java.util.Hashtable;
import java.util.Map;

public class RegistrySingleton {

  private static Map<String, RegistrySingleton> m_reg = new Hashtable<>();

  // 无名的静态构造函数
  static {
    RegistrySingleton reg = new RegistrySingleton();
    m_reg.put(reg.getClass().getName(), reg);
  }

  public static RegistrySingleton getInstance(String name) {
    if (name == null)
      name = "singleton.RegistrySingleton";
    if (m_reg.get(name) == null) {
      try {
        m_reg.put(name, (RegistrySingleton) Class.forName(name).newInstance());
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    return m_reg.get(name);
  }

  public void about() {
    System.out.println("这个一父类的商业方法示例！");
  }

  /**
   * @param args
   */
  public static void main(String[] args) {
    RegistrySingleton reg = RegistrySingleton.getInstance(null);
    reg.about();
  }

}
